﻿#include "tools.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QCheckBox>
#include <QPushButton>
#include <QScrollArea>
#include "utils.h"

struct TNavItem
{
    QString icon;
    QString title;
    QString tip;

    TNavItem(QString _icon, QString _title, QString _tip)
    {
        icon = _icon;
        title = _title;
        tip = _tip;
    }
};
typedef QList<TNavItem> NavList;


QWidget *CreateNavItem(TNavItem tItem)
{
    QString icon = tItem.icon;
    QString title = tItem.title;
    QString tip = tItem.tip;
    QPushButton *item = new QPushButton();
    item->setCursor(Qt::PointingHandCursor);
    item->setStyleSheet("QPushButton{border-style:none;background: #fff; border: 0; }"
                  "QPushButton:hover {background: #fffcf8; border: 1px solid #ffe7c1; }");
    item->setFixedHeight(64);

    QLabel *labelIcon = new QLabel();
    QLabel *labelTitle = CreateCustomLabel(title, "#444444", 14);
    QLabel *labelTip = CreateCustomLabel(tip, "#777777", 12);

    labelIcon->setPixmap(QPixmap(icon));
    labelIcon->setFixedWidth(40);

    QVBoxLayout *layoutRight = new QVBoxLayout();
    layoutRight->addStretch(1);
    layoutRight->addWidget(labelTitle);
    layoutRight->addWidget(labelTip);
    layoutRight->addStretch(1);
    layoutRight->setSpacing(2);
    layoutRight->setContentsMargins(6, 0, 0, 0);

    QHBoxLayout *layoutMain = new QHBoxLayout(item);
    layoutMain->addWidget(labelIcon);
    layoutMain->addLayout(layoutRight);
    layoutMain->setContentsMargins(10, 10, 10, 10);
    layoutMain->setSpacing(10);

    return item;
}

QWidget *CreateNavGroup(QString title, NavList list)
{
    QWidget *item = new QWidget();
    QLabel *labelTitle = new QLabel("  " + title);
    labelTitle->setStyleSheet("border: 3px solid #c0a686; border-top-color: rgba(0,0,0,0); border-right-color: rgba(0,0,0,0); border-bottom-color: rgba(0,0,0,0); font-size: 14px");

    QGridLayout *layoutItems = new QGridLayout();
    layoutItems->setColumnStretch(0, 1);
    layoutItems->setColumnStretch(1, 1);
    layoutItems->setColumnStretch(2, 1);

    int nRow = 0;
    int nCol = 0;
    foreach (TNavItem var, list)
    {
        layoutItems->addWidget(CreateNavItem(var), nRow, nCol);
        nCol++;

        if (nCol > 2)
        {
            nCol = 0;
            nRow++;
        }
    }

    QVBoxLayout *layoutMain = new QVBoxLayout(item);
    layoutMain->addWidget(labelTitle);
    layoutMain->addLayout(layoutItems);
    layoutMain->setContentsMargins(20, 10, 20, 10);
    layoutMain->setSpacing(10);

    return item;
}

Tools::Tools(QWidget *parent) : BaseChildPane(parent)
{
    m_strTitle = "安全工具";

    InitCtrl();
}

void Tools::InitCtrl()
{
    QScrollArea *pScrollArea = new QScrollArea(this);
    pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//隐藏横向滚动条
    pScrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    pScrollArea->setWidgetResizable(true);


    QWidget *widgetContent = new QWidget(pScrollArea);
    widgetContent->setObjectName("widgetContent");
    pScrollArea->setWidget(widgetContent);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(pScrollArea);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    setLayout(mainLayout);

    widgetContent->setStyleSheet(".QWidget#widgetContent{background: #fff; border-top: 1px solid #f0f0f0; }");
    pScrollArea->setStyleSheet("QScrollArea{background: #fff; border: 0; }");

    QVBoxLayout *widgetContentLayout = new QVBoxLayout(widgetContent);

    {
        NavList list;
        list << TNavItem(":/img/100/tools/leak_repair.png", "漏洞修复", "扫描并修复系统漏洞")
            << TNavItem(":/img/100/tools/sys_repair.png", "系统修复", "修复因病毒等导致的系统异常")
            << TNavItem(":/img/100/tools/pop_block.png", "弹窗拦截", "拦载程序推送的不受欢迎弹窗")
            << TNavItem(":/img/100/tools/sys_clean.png", "垃圾清理", "清理各种系统垃圾")
            << TNavItem(":/img/100/tools/leak_repair.png", "启动项管理", "管理名类软件的开机自启动")
            << TNavItem(":/img/100/tools/autoruns.png", "文件粉碎", "强制刷除或彻底粉碎文件")
        << TNavItem(":/img/100/tools/right_click.png", "右键管理", "管理文件、桌面、IE右键菜单");
        widgetContentLayout->addWidget(CreateNavGroup("系统工具", list));
    }

    {
        NavList list;
        list << TNavItem(":/img/100/tools/net_repair.png", "断网修复", "检侧并修复新网问题")
            << TNavItem(":/img/100/tools/net_speed.png", "流量监控", "检视及控制程序的网络流量")
            << TNavItem(":/img/100/tools/host.png", "修改HOST文件", "修改负责域名快速解析的文件");
        widgetContentLayout->addWidget(CreateNavGroup("网络工具", list));
    }

    {
        NavList list;
        list << TNavItem(":/img/100/tools/sword.png", "火绒剑", "安全专业人士的分析工具")
            << TNavItem(":/img/100/tools/kill.png", "专杀工具", "查杀名类恶性顽固病毒");
        widgetContentLayout->addWidget(CreateNavGroup("高级工具", list));
    }

    widgetContentLayout->addStretch(1);
    widgetContentLayout->setContentsMargins(0, 0, 0, 0);
    widgetContentLayout->setSpacing(10);
}
